package be.sc.classicnetwork.dao.hibernate;

import java.util.List;

import org.hibernate.criterion.Order;

import be.sc.classicnetwork.dao.ContactDao;
import be.sc.classicnetwork.model.Car;
import be.sc.classicnetwork.model.Contact;

/**
 * ContactDaoImpl.
 */
public class ContactDaoImpl extends HibernateGenericDao<Contact> implements
		ContactDao {

	/**
	 * Constructor.
	 */
	public ContactDaoImpl() {
		super();
	}

	/**
	 * @param clazz
	 */
	public ContactDaoImpl(Class<Contact> clazz) {
		super(clazz);
	}

	/**
	 * @see be.sc.classicnetwork.dao.hibernate.HibernateGenericDao#findByIdFull(int)
	 */
	@Override
	public Contact findByIdFull(String id) {
		Contact contact = super.findByIdFull(id);

		if (contact == null) {
			return null;
		}

		// lazy load many-to-many relations
		getHibernateTemplate().initialize(contact.getMakes());
		getHibernateTemplate().initialize(contact.getSites());

		return contact;
	}

	/**
	 * Unpublishes the specified contact.
	 */
	@Override
	public void delete(Contact entity) {
		entity.setPublished(false);
		this.saveOrUpdate(entity);
	}

	/**
	 * @see be.sc.classicnetwork.dao.hibernate.HibernateGenericDao#findAll(int,
	 *      int)
	 */
	@Override
	public List<Contact> findAll(int first, int max) {
		getHibernateTemplate().enableFilter(Car.PUBLISHED_FILTER);
		return findByCriteria(0, 0, null, null, new Order[] { Order
				.asc("lastName") });
	}

}
